package com.fessc.develop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fessc.develop.entity.dbvo.TPayment;
import com.fessc.develop.entity.ccbvo.*;
import com.fessc.develop.entity.comvo.*;
import com.fessc.develop.mapper.TPaymentMapper;
import com.fessc.develop.model.enums.EnumCallBackOption;
import com.fessc.develop.model.enums.EnumPaymentStatus;
import com.fessc.develop.service.interfaces.ITPaymentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fessc.develop.utils.BusiUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author y
 * @since 2024-06-26
 */
@Service
public class TPaymentServiceImpl extends ServiceImpl<TPaymentMapper, TPayment> implements ITPaymentService {

    private Map<String, String> paymentStatusMap;

    @PostConstruct
    void init() {
        paymentStatusMap = new HashMap<>();
        paymentStatusMap.put(EnumPaymentStatus.PAYMENT_SUCCESS.toString(), "1"); //付款成功
        paymentStatusMap.put(EnumPaymentStatus.SEND.toString(), "3"); //付款处理中
        paymentStatusMap.put(EnumPaymentStatus.TO_PRE_PAY.toString(), "3"); //付款处理中
        paymentStatusMap.put(EnumPaymentStatus.TO_SEND_TO_BANK.toString(), "3"); //付款处理中
    }

    @Resource
    private TPaymentMapper tPaymentMapper;

    /**
     * 根据银行编号生成批次号
     *
     * @param bankid 银行编号
     * @return 批次号
     */
    @Override
    public String getPaymentBatchNo(String bankid, int len) {
        String batchNo = "";
        do {
            batchNo = BusiUtil.getSerialNumber(bankid, "0123456789", len);
            QueryWrapper<TPayment> paymentQueryWrapper = new QueryWrapper<>();
            paymentQueryWrapper.eq("payment_batch_no", batchNo);
            paymentQueryWrapper.or().eq("refund_batch_no", batchNo);
            List<TPayment> tPaymentList = this.list(paymentQueryWrapper);
            if (!tPaymentList.isEmpty()) {
                batchNo = "";
            }
        } while (BusiUtil.isNull(batchNo));
        return batchNo;
    }

    /**
     * 根据子订单号和支付状态，查询预付款记录集
     *
     * @param subOrders     预付款订单号
     * @param paymentStatus 发送银行状态
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getPaymentBySubOrderNoAndPayStatus(List<String> subOrders, String paymentStatus) {
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        if (BusiUtil.isNotNull(paymentStatus)) {
            queryWrapper.eq("payment_status", paymentStatus);
        }
        queryWrapper.in("suborderno", subOrders);
        return this.list(queryWrapper);
    }

    /**
     * 根据银行编号和支付状态，查询预付款记录集
     *
     * @param paymentType   支付类型
     * @param paymentStatus 发送银行状态
     * @param bankId        银行编号
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getPaymentByTypeAndStatus(String paymentType, String paymentStatus, String bankId) {
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("payment_type", paymentType).eq("payment_status", paymentStatus);
        if (BusiUtil.isNotNull(bankId)) {
            queryWrapper.ne("bankid", bankId);
        }
        return this.list(queryWrapper);
    }

    /**
     * 根据银行编号，查询预付款记录集
     *
     * @param bankId      银行编号
     * @param termDate    查询时间
     * @param containType 包含条件
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getPaymentByBankIdToPush(String bankId, String termDate, String containType) {
        /*
        LambdaQueryWrapper<TPayment> queryWrapper = new LambdaQueryWrapper<>();
        if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "Y")) {
            queryWrapper.eq(TPayment::getBankid, bankId);
        } else if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "N")) {
            queryWrapper.ne(TPayment::getBankid, bankId);
        }
        queryWrapper.and(qr -> qr.and(qr1 -> qr1.eq(TPayment::getPaymentType, "B").eq(TPayment::getPaymentStatus, "B"))
                .or(qr2 -> qr2.eq(TPayment::getPaymentType, "G").eq(TPayment::getPaymentStatus, "G")));
        */
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("payment_status", "B").eq("payment_type", "B");
        if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "Y")) {
            queryWrapper.eq("bankid", bankId);
        } else if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "N")) {
            queryWrapper.ne("bankid", bankId);
        }
        if (BusiUtil.isNotNull(termDate)) {
            queryWrapper.ge("insert_date", termDate);
        }
        return this.list(queryWrapper);
    }

    /**
     * 根据银行编号，查询预付款记录集
     *
     * @param bankId      银行编号
     * @param termDate    查询时间
     * @param containType 包含条件
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getPrePaymentByContainType(String bankId, String termDate, String containType) {
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("payment_status", "B")
                .eq("payment_type", "B")
                .apply("(payment_batch_no = '' or payment_batch_no is null)");
        if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "Y")) {
            queryWrapper.eq("bankid", bankId);
        } else if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "N")) {
            queryWrapper.ne("bankid", bankId);
        }
        if (BusiUtil.isNotNull(termDate)) {
            queryWrapper.ge("insert_date", termDate);
        }
        return this.list(queryWrapper);
    }

    /**
     * 根据银行编号，查询预付款记录集
     *
     * @param bankId      银行编号
     * @param termDate    查询时间
     * @param containType 查询条件
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getPrePaymentByContainTypeToPush(String bankId, String termDate, String containType) {
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("payment_status", "B")
                .eq("payment_type", "B")
                .isNotNull("payment_batch_no")
                .apply("ifnull(payment_batch_no,'') != ''");
        if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "Y")) {
            queryWrapper.eq("bankid", bankId);
        } else if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "N")) {
            queryWrapper.ne("bankid", bankId);
        }
        if (BusiUtil.isNotNull(termDate)) {
            queryWrapper.ge("insert_date", termDate);
        }
        return this.list(queryWrapper);
    }

    /**
     * 根据银行编号，查询预付款退款记录集
     *
     * @param bankId      银行编号
     * @param termDate    查询时间
     * @param containType 查询条件
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getRefundPaymentByContainToPush(String bankId, String termDate, String containType) {
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("refund", "Y")
                .eq("payment_type", "B")
                .eq("verify_status", "N")
                .isNotNull("refund_batch_no")
                .apply("ifnull(refund_batch_no,'') != ''");
        if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "Y")) {
            queryWrapper.eq("bankid", bankId);
        } else if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "N")) {
            queryWrapper.ne("bankid", bankId);
        }
        if (BusiUtil.isNotNull(termDate)) {
            queryWrapper.ge("refund_date", termDate);
        }
        return this.list(queryWrapper);
    }

    /**
     * 根据银行编号，查询预付款待更新状态记录集
     *
     * @param bankId      银行编号
     * @param termDate    查询时间
     * @param containType 包含条件
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getPaymentToUpdateStatusByBankId(String bankId, String termDate, String containType) {
        /*
        LambdaQueryWrapper<TPayment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(qr -> qr.eq(TPayment::getPaymentStatus, EnumPaymentStatus.SEND.toString()));
        queryWrapper.and(qr1 -> qr1.eq(TPayment::getPaymentType, "B").or().eq(TPayment::getPaymentType, "G"));
        if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "Y")) {
            queryWrapper.eq(TPayment::getBankid, bankId);
        } else if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "N")) {
            queryWrapper.ne(TPayment::getBankid, bankId);
        }
        */
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("payment_status", EnumPaymentStatus.SEND.toString()).eq("payment_type", "B");
        if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "Y")) {
            queryWrapper.eq("bankid", bankId);
        } else if (BusiUtil.isNotNull(bankId) && BusiUtil.isEquals(containType, "N")) {
            queryWrapper.ne("bankid", bankId);
        }
        if (BusiUtil.isNotNull(termDate)) {
            queryWrapper.ge("insert_date", termDate);
        }
        return this.list(queryWrapper);
    }

    /**
     * 根据银行编号，查询预付款退款记录集
     *
     * @param bankId      银行编号
     * @param termDate    查询时间
     * @param containType 包含条件
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getRefundPaymentToUpdateStatusByBankId(String bankId, String termDate, String containType) {
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("refund", "Y")
                .eq("payment_type", "B")
                .eq("verify_status", "Y")
                .isNull("refund_status");
        if (BusiUtil.isEquals(containType, "Y")) {
            queryWrapper.eq("bankid", bankId);
        } else if (BusiUtil.isEquals(containType, "N")) {
            queryWrapper.ne("bankid", bankId);
        }
        if (BusiUtil.isNotNull(termDate)) {
            queryWrapper.ge("refund_date", termDate);
        }
        return this.list(queryWrapper);
    }

    /**
     * 根据子订单编号，查询预付款记录集
     *
     * @param orderNo 订单编号
     * @return 预付款记录
     */
    @Override
    public TPayment getTPaymentByOrderNo(String orderNo) {
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("suborderno", orderNo);
        return this.getOne(queryWrapper);
    }

    /**
     * 根据订单编号，跨境付汇子订单记录集
     *
     * @param orderno 订单编号
     * @return 预付款记录
     */
    @Override
    public List<TPayment> getSettleTradePaymentList(String orderno) {

        return tPaymentMapper.getSettleTradePaymentList(orderno);
    }

    /**
     * 根据预付款记录集，生成发送报文体
     *
     * @param tPaymentList 预付款集合
     * @return 发送报文集合
     */
    public PrePaymentBodyVo fakePrePaymentSendBody(List<TPayment> tPaymentList) {
        List<PrePaymentOrderVo> paymentOrderList = new ArrayList<>();
        PrePaymentBodyVo body = new PrePaymentBodyVo();
        tPaymentList.forEach(tPayment -> {
            PrePaymentOrderVo orderVo = new PrePaymentOrderVo();
            BeanUtils.copyProperties(tPayment, orderVo);
            orderVo.setOrderno(tPayment.getSuborderno());
            paymentOrderList.add(orderVo);
            body.setOrgCode(tPayment.getOrgCode());
            body.setBankid(tPayment.getBankid().toString());
            body.setPaymentBatchNo(tPayment.getPaymentBatchNo());
        });
        body.setPaymentOrderList(paymentOrderList);
        body.setTotalCount(String.valueOf(paymentOrderList.size()));
        return body;
    }

    /**
     * 根据预付款记录集，生成发送报文体
     *
     * @param tPaymentList 预付款集合
     * @return 发送报文集合
     */
    public PrePaymentRefundBodyVo fakePrePaymentRefundSendBody(List<TPayment> tPaymentList) {
        List<PrePaymentRefundOrderVo> refundOrderList = new ArrayList<>();
        PrePaymentRefundBodyVo body = new PrePaymentRefundBodyVo();
        tPaymentList.forEach(tPayment -> {
            PrePaymentRefundOrderVo order = new PrePaymentRefundOrderVo();
            BeanUtils.copyProperties(tPayment, order);
            refundOrderList.add(order);
            body.setOrgCode(tPayment.getOrgCode());
            body.setBankid(tPayment.getBankid().toString());
            body.setRefundBatchNo(tPayment.getRefundBatchNo());
        });
        body.setRefundOrderList(refundOrderList);
        body.setTotalCount(String.valueOf(refundOrderList.size()));
        return body;
    }

    /**
     * 根据预付款记录集和交易类型，生成发送报文体（建行）
     *
     * @param tPayment 预付款记录
     * @param busType  业务类型
     * @return 发送报文集合
     */
    public CCBPrePaymentBodyVo fakeCCBPrePaymentSendBody(TPayment tPayment, String busType) {
        CCBPrePaymentBodyVo bodyVo = new CCBPrePaymentBodyVo();
        BeanUtils.copyProperties(tPayment, bodyVo);
        //建行预付款扣款订单时间取insert_date，退款订单时间取refund_date
        if (BusiUtil.isEquals(EnumCallBackOption.PRE_REFUND_CALL_BACK.toString(), busType)) {
            bodyVo.setInsertDate(tPayment.getRefundDate());
        }
        bodyVo.setBsnTp(busType);
        bodyVo.setPayercurrency(BusiUtil.getEnumSubString(bodyVo.getPayercurrency()));  //处理币种，如果数据库保存的是156人民币，只取156
        CCBPrePaymentVendeeVo vendeeVo = new CCBPrePaymentVendeeVo();
        BeanUtils.copyProperties(tPayment, vendeeVo);
        vendeeVo.setNat(BusiUtil.getEnumSubString(vendeeVo.getNat()));    //处理国籍，如果数据库保存的是704越南，只取704
        CCBPrePaymentVendorVo vendorVo = new CCBPrePaymentVendorVo();
        BeanUtils.copyProperties(tPayment, vendorVo);
        vendorVo.setPayeecountry(BusiUtil.getEnumSubString(vendorVo.getPayeecountry()));    //处理国籍，如果数据库保存的是704越南，只取704
        CCBPrePaymentItemVo itemVo = new CCBPrePaymentItemVo();
        BeanUtils.copyProperties(tPayment, itemVo);
        List<CCBPrePaymentItemVo> ccbOrderItemVo = new ArrayList<>();
        ccbOrderItemVo.add(itemVo);
        bodyVo.setCcbPrePaymentVendeeVo(vendeeVo);
        bodyVo.setCcbPrePaymentVendorVo(vendorVo);
        bodyVo.setCcbOrderItems(ccbOrderItemVo);
        return bodyVo;
    }

    /**
     * 根据预付款退款记录集和交易类型，生成发送报文体（建行）
     *
     * @param tPayment 预付款记录
     * @param busType  业务类型
     * @return 发送报文集合
     */
    public CCBPrePaymentBodyVo fakeCCBPrePaymentRefundSendBody(TPayment tPayment, String busType) {
        CCBPrePaymentBodyVo bodyVo = new CCBPrePaymentBodyVo();
        BeanUtils.copyProperties(tPayment, bodyVo);
        //建行预付款扣款订单时间取insert_date，退款订单时间取refund_date
        if (BusiUtil.isEquals(EnumCallBackOption.PRE_REFUND_CALL_BACK.toString(), busType)) {
            bodyVo.setInsertDate(tPayment.getRefundDate());
        }
        bodyVo.setBsnTp(busType);
        bodyVo.setPayercurrency(BusiUtil.getEnumSubString(bodyVo.getPayercurrency()));  //处理币种，如果数据库保存的是156人民币，只取156
        CCBPrePaymentVendeeVo vendeeVo = new CCBPrePaymentVendeeVo();
        BeanUtils.copyProperties(tPayment, vendeeVo);
        vendeeVo.setNat(BusiUtil.getEnumSubString(vendeeVo.getNat()));    //处理国籍，如果数据库保存的是704越南，只取704
        CCBPrePaymentVendorVo vendorVo = new CCBPrePaymentVendorVo();
        BeanUtils.copyProperties(tPayment, vendorVo);
        vendorVo.setPayeecountry(BusiUtil.getEnumSubString(vendorVo.getPayeecountry()));    //处理国籍，如果数据库保存的是704越南，只取704
        CCBPrePaymentItemVo itemVo = new CCBPrePaymentItemVo();
        BeanUtils.copyProperties(tPayment, itemVo);
        List<CCBPrePaymentItemVo> ccbOrderItemVo = new ArrayList<>();
        ccbOrderItemVo.add(itemVo);
        bodyVo.setCcbPrePaymentVendeeVo(vendeeVo);
        bodyVo.setCcbPrePaymentVendorVo(vendorVo);
        bodyVo.setCcbOrderItems(ccbOrderItemVo);
        return bodyVo;
    }

    /**
     * 根据预付款待查询状态记录集，生成发送报文体
     *
     * @param tPaymentList 预付款记录
     * @param operType     业务类型
     * @return 发送报文集合
     */
    @Override
    public PrePaymentRefundStatusVo fakePrePaymentStatusSendBody(List<TPayment> tPaymentList, String operType) {
        PrePaymentRefundStatusVo bodyVo = new PrePaymentRefundStatusVo();
        if (BusiUtil.isEquals(EnumCallBackOption.PRE_PAYMENT_STATUS.toString(), operType)) {
            bodyVo.setBatchSerialNo(tPaymentList.get(0).getPaymentBatchNo());
        } else if (BusiUtil.isEquals(EnumCallBackOption.PRE_REFUND_STATUS.toString(), operType)) {
            bodyVo.setBatchSerialNo(tPaymentList.get(0).getRefundBatchNo());
        }
        /*
        if (tPaymentList.size() == 1) {
            bodyVo.setOrderNo(tPaymentList.get(0).getSuborderno());
        }
        */
        bodyVo.setOperType(operType);
        bodyVo.setBankid(tPaymentList.get(0).getBankid().toString());
        return bodyVo;
    }

    /**
     * 更新预付款状态
     *
     * @param tPayment      预付款集合
     * @param paymentStatus 交易状态
     * @return 更新成功失败标志
     */
    @Override
    public Boolean updatePrePaymentStatusRefidsLog(TPayment tPayment, String paymentStatus) {
        UpdateWrapper<TPayment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("suborderno", tPayment.getSuborderno());
        if (BusiUtil.isNotNull(tPayment.getId())) {
            updateWrapper.eq("id", tPayment.getId());
        }
        if (BusiUtil.isEquals(EnumPaymentStatus.SEND.toString(), paymentStatus)) {  //已经发送银行
            // 更新预付款发送银行状态
            updateWrapper.set("payment_status", tPayment.getPaymentStatus());
        } else if (BusiUtil.isEquals(EnumPaymentStatus.PAYMENT_SUCCESS.toString(), paymentStatus)) {  //交易成功
            // 更新预付款交易成功状态
            if (BusiUtil.isNotNull(tPayment.getPaymentStatus())) {
                updateWrapper.set("payment_status", tPayment.getPaymentStatus());
            }
            if (BusiUtil.isNotNull(tPayment.getPaymentLog())) {
                updateWrapper.set("payment_log", tPayment.getPaymentLog());
            }
            if (BusiUtil.isNotNull(tPayment.getBankRefids())) {
                updateWrapper.set("bank_refids", tPayment.getBankRefids());
            }
            if (BusiUtil.isNotNull(tPayment.getPaymentWorkdate())) {
                updateWrapper.set("payment_workdate", tPayment.getPaymentWorkdate());
            }
        } else if (BusiUtil.isEquals(EnumPaymentStatus.PAYMENT_FAIL.toString(), paymentStatus)) {  //交易失败
            // 更新预付款交易失败状态
            if (BusiUtil.isNotNull(tPayment.getPaymentStatus())) {
                updateWrapper.set("payment_status", tPayment.getPaymentStatus());
            }
            if (BusiUtil.isNotNull(tPayment.getPaymentLog())) {
                updateWrapper.set("payment_log", tPayment.getPaymentLog());
            }
        }
        return this.update(updateWrapper);
    }

    /**
     * 更新预付款退款状态
     *
     * @param tPayment      预付款记录
     * @param paymentStatus 交易状态
     * @return 更新成功失败标志
     */
    @Override
    public Boolean updateRefundStatusRefidsLog(TPayment tPayment, String paymentStatus) {
        UpdateWrapper<TPayment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("suborderno", tPayment.getSuborderno());
        if (BusiUtil.isNotNull(tPayment.getId())) {
            updateWrapper.eq("id", tPayment.getId());
        }
        if (BusiUtil.isEquals(EnumPaymentStatus.SEND.toString(), paymentStatus)) {  //已经发送银行
            // 更新预付款退款发送银行状态
            updateWrapper.set("verify_status", tPayment.getVerifyStatus());
        } else if (BusiUtil.isEquals(EnumPaymentStatus.PAYMENT_SUCCESS.toString(), paymentStatus)) {  //交易成功
            // 更新预付款退款交易成功状态
            if (BusiUtil.isNotNull(tPayment.getRefundStatus())) {
                updateWrapper.set("refund_status", tPayment.getRefundStatus());
            }
            if (BusiUtil.isNotNull(tPayment.getRefundLog())) {
                updateWrapper.set("refund_log", tPayment.getRefundLog());
            }
            if (BusiUtil.isNotNull(tPayment.getRefundRefids())) {
                updateWrapper.set("refund_refids", tPayment.getRefundRefids());
            }
            if (BusiUtil.isNotNull(tPayment.getSend2())) {
                updateWrapper.set("send2", tPayment.getSend2());
            }
            if (BusiUtil.isNotNull(tPayment.getRefundWorkdate())) {
                updateWrapper.set("refund_workdate", tPayment.getRefundWorkdate());
            }
        } else if (BusiUtil.isEquals(EnumPaymentStatus.PAYMENT_FAIL.toString(), paymentStatus)) {  //交易失败
            // 更新预付款退款发送交易失败状态
            if (BusiUtil.isNotNull(tPayment.getRefundStatus())) {
                updateWrapper.set("refund_status", tPayment.getRefundStatus());
            }
            if (BusiUtil.isNotNull(tPayment.getRefundLog())) {
                updateWrapper.set("refund_log", tPayment.getRefundLog());
            }
            if (BusiUtil.isNotNull(tPayment.getVerifyStatus())) {
                updateWrapper.set("verify_status", tPayment.getVerifyStatus());
            }
        }
        return this.update(updateWrapper);
    }

    /**
     * 批量更新预付款发送银行状态
     *
     * @param paymentStatus  交易状态
     * @param paymentLog     交易结果描述
     * @param bankRefids     银行流水号
     * @param paymentBatchNo 批次号
     */
    @Override
    public void updatePrePaymentStatusByPaymentBatchNo(String paymentStatus, String paymentLog, String bankRefids, String paymentBatchNo) {
        UpdateWrapper<TPayment> updateWrapper = new UpdateWrapper<>();
        if (BusiUtil.isNotNull(paymentStatus)) {
            updateWrapper.set("payment_status", paymentStatus);
        }
        if (BusiUtil.isNotNull(bankRefids)) {
            updateWrapper.set("bank_refids", bankRefids);
        }
        if (BusiUtil.isNotNull(paymentLog)) {
            updateWrapper.set("payment_log", paymentLog);
        }
        updateWrapper.eq("payment_batch_No", paymentBatchNo);
        this.update(updateWrapper);
    }

    /**
     * 根据流水号批量更新预付款交易状态
     *
     * @param tPaymentList 订单编号
     */
    @Override
    public void updatePrePaymentStatusByOrderNo(List<TPayment> tPaymentList) {
        tPaymentMapper.updatePrePaymentStatusByOrderNo(tPaymentList);
    }

    /**
     * 批量更新预付款退款发送银行状态
     *
     * @param verifyStatus  退款审核状态
     * @param refundStatus  退款状态
     * @param refundLog     交易结果描述
     * @param refundRefids  银行流水号
     * @param send2         预付款退款发送状态
     * @param refundBatchNo 批次号
     */
    @Override
    public void updatePrePaymentRefundByPaymentBatchNo(String verifyStatus, String refundStatus, String refundLog, String refundRefids, String send2, String refundBatchNo) {
        UpdateWrapper<TPayment> updateWrapper = new UpdateWrapper<>();
        if (BusiUtil.isNotNull(verifyStatus)) {
            updateWrapper.set("verify_status", verifyStatus);
        }
        if (BusiUtil.isNotNull(refundStatus)) {
            updateWrapper.set("refund_status", refundStatus);
        }
        if (BusiUtil.isNotNull(refundLog)) {
            updateWrapper.set("refund_log", refundLog);
        }
        if (BusiUtil.isNotNull(refundRefids)) {
            updateWrapper.set("refund_refids", refundRefids);
        }
        if (BusiUtil.isNotNull(send2)) {
            updateWrapper.set("send2", send2);
        }
        updateWrapper.eq("refund_batch_No", refundBatchNo);
        this.update(updateWrapper);
    }

    /**
     * 根据流水号批量更新预付款退款交易状态
     *
     * @param tPaymentList 订单编号
     */
    @Override
    public void updatePrePaymentRefundStatusByOrderNo(List<TPayment> tPaymentList) {
        tPaymentMapper.updatePrePaymentRefundStatusByOrderNo(tPaymentList);
    }

    /**
     * 获取预付款退款的数据
     *
     * @param bankId      银行编号
     * @param termDate    查询时间
     * @param containType 包含条件
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getRefundPaymentByBankIdToPush(String bankId, String termDate, String containType) {
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("refund", "Y")
                .eq("payment_type", "B")
                .eq("verify_status", "N");
        if (BusiUtil.isEquals(containType, "Y")) {
            queryWrapper.eq("bankid", bankId);
        } else if (BusiUtil.isEquals(containType, "N")) {
            queryWrapper.ne("bankid", bankId);
        }
        if (BusiUtil.isNotNull(termDate)) {
            queryWrapper.ge("refund_date", termDate);
        }
        return this.list(queryWrapper);
    }

    /**
     * 获取预付款退款的数据
     *
     * @param bankId      银行编号
     * @param termDate    查询时间
     * @param containType 包含条件
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getRefundPaymentByContainType(String bankId, String termDate, String containType) {
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("refund", "Y")
                .eq("payment_type", "B")
                .eq("verify_status", "N")
                .apply("(refund_batch_no = '' or refund_batch_no is null)");
        if (BusiUtil.isEquals(containType, "Y")) {
            queryWrapper.eq("bankid", bankId);
        } else if (BusiUtil.isEquals(containType, "N")) {
            queryWrapper.ne("bankid", bankId);
        }
        if (BusiUtil.isNotNull(termDate)) {
            queryWrapper.ge("refund_date", termDate);
        }
        return this.list(queryWrapper);
    }

    /**
     * 获取指定日期对账数据
     * <p>
     * 查询指定日期，payment_type=’B‘（银行） + payment_status ！=’B‘（已发送银行） + refund=’N‘（非退款的交易）
     *
     * @param settleDate 订单时间
     * @param bankId     银行编号
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getPrePaymentCheckingList(String settleDate, String bankId) {
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DATE(insert_date)", settleDate)
                .eq("payment_type", "B")
                .ne("payment_status", "B")
                .eq("refund", "N");
        if (BusiUtil.isNotNull(bankId)) {
            queryWrapper.eq("bankid", bankId);
        }
        return this.list(queryWrapper);
    }

    /**
     * 获取指定日期的预付款退款对账数据
     *
     * @param settleDate 订单时间
     * @param bankId     银行编号
     * @return 预付款集合
     */
    @Override
    public List<TPayment> getRefundCheckingList(String settleDate, String bankId) {
        QueryWrapper<TPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DATE(insert_date)", settleDate)
                .eq("payment_type", "B")
                .ne("payment_status", "B")
                .ne("verify_status", "N")
                .eq("refund", "Y");
        if (BusiUtil.isNotNull(bankId)) {
            queryWrapper.eq("bankid", bankId);
        }
        return this.list(queryWrapper);
    }

    /**
     * 更新预付款批次号
     *
     * @param tPaymentList 预付款集合
     */
    @Override
    public void updatePaymentBatchNoById(List<TPayment> tPaymentList) {
        tPaymentMapper.updatePaymentBatchNoById(tPaymentList);
    }

    /**
     * 更新预付款退款批次号
     *
     * @param tPaymentList 预付款集合
     */
    @Override
    public void updateRefundBatchNoById(List<TPayment> tPaymentList) {
        tPaymentMapper.updateRefundBatchNoById(tPaymentList);
    }

}
